﻿using FangChanCrm.Services.Interface;
using FangChanCrmEF;
using FangChanCrmModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace FangChanCrm.Services.Realization
{
    public class t_AnChangBiaoService : It_AnChangBiaoService
    {
        public readonly MyContext _Context;

        public t_AnChangBiaoService(MyContext context)
        {
            _Context = context;
        }

        public int Add(t_AnChangBiao t_AnChangBiao)
        {
            _Context.t_AnChangBiao.Add(t_AnChangBiao);
            return _Context.SaveChanges();
        }

        public t_AnChangBiao GetId(int id)
        {
            return _Context.t_AnChangBiao.FirstOrDefault(x => x.Id == id);
        }

        public List<t_AnChangBiao> GetList()
        {
            return _Context.t_AnChangBiao.Where(x => x.Id != 1).ToList();
        }

        public string GetListWithFenYe(t_AnChangBiao t_AnChangBiao, int start, int limit, string dir, int column)
        {
            var returnwhere = ReturnWhere(t_AnChangBiao);
            var list = new List<t_AnChangBiao>();
            if (returnwhere.Length > 0)
            {
                list = _Context.t_AnChangBiao.Where(returnwhere).ToList();
            }
            else
            {
                list = _Context.t_AnChangBiao.ToList();
            }
            switch (column)
            {
                case 0:
                    break;
                case 1:
                    if (dir == "asc")
                    {
                        list = list.OrderBy(x => x.AnChangMingCheng).ToList();
                    }
                    else
                    {
                        list = list.OrderByDescending(x => x.AnChangMingCheng).ToList();
                    }
                    break;
                case 2:
                    if (dir == "asc")
                    {
                        list = list.OrderBy(x => x.ZhuangTai).ToList();
                    }
                    else
                    {
                        list = list.OrderByDescending(x => x.ZhuangTai).ToList();
                    }
                    break;
                default:
                    break;
            }
            return Helper.ListToJsonAddFenYe(list, start, limit);
        }

        public string ReturnWhere(t_AnChangBiao t_AnChangBiao)
        {
            string returnstr = "id != 1";
            if (t_AnChangBiao.BeiZhu1 != null)
            {
                returnstr = returnstr + " and BeiZhu1.Contains(" + t_AnChangBiao.BeiZhu1 + ")";
            }
            if (t_AnChangBiao.BeiZhu2 != null)
            {
                returnstr = returnstr + " and BeiZhu2.Contains(" + t_AnChangBiao.BeiZhu2 + ")";
            }
            if (t_AnChangBiao.BeiZhu3 != null)
            {
                returnstr = returnstr + " and BeiZhu3.Contains(" + t_AnChangBiao.BeiZhu3 + ")";
            }
            if (t_AnChangBiao.BeiZhu4 != null)
            {
                returnstr = returnstr + " and BeiZhu4.Contains(" + t_AnChangBiao.BeiZhu4 + ")";
            }
            if (t_AnChangBiao.BeiZhu5 != null)
            {
                returnstr = returnstr + " and BeiZhu5.Contains(" + t_AnChangBiao.BeiZhu5 + ")";
            }
            if (t_AnChangBiao.BeiZhu6 != null)
            {
                returnstr = returnstr + " and BeiZhu6.Contains(" + t_AnChangBiao.BeiZhu6 + ")";
            }
            if (t_AnChangBiao.BeiZhu7 != null)
            {
                returnstr = returnstr + " and BeiZhu7.Contains(" + t_AnChangBiao.BeiZhu7 + ")";
            }
            if (t_AnChangBiao.BeiZhu8 != null)
            {
                returnstr = returnstr + " and BeiZhu8.Contains(" + t_AnChangBiao.BeiZhu8 + ")";
            }
            if (t_AnChangBiao.BeiZhu9 != null)
            {
                returnstr = returnstr + " and BeiZhu9.Contains(" + t_AnChangBiao.BeiZhu9 + ")";
            }
            if (t_AnChangBiao.BeiZhu10 != null)
            {
                returnstr = returnstr + " and BeiZhu10.Contains(" + t_AnChangBiao.BeiZhu10 + ")";
            }
            if (t_AnChangBiao.AnChangMingCheng != null && t_AnChangBiao.AnChangMingCheng != "")
            {
                returnstr = returnstr + " and ZhanDuiMingCheng.Contains(\"" + t_AnChangBiao.AnChangMingCheng + "\")";
            }
            if (t_AnChangBiao.ZhuangTai != null && t_AnChangBiao.ZhuangTai != "")
            {
                returnstr = returnstr + " and ZhuangTai.Contains(\"" + t_AnChangBiao.ZhuangTai + "\")";
            }
            return returnstr;
        }


    }
}
